<script setup lang="ts">
import {inject, onBeforeUpdate, onMounted, reactive, ref} from "vue";
import {chat_send, SocketData} from "@/data/SocketData";
import {getFullTime} from "@/utils/moment";
import {useUserStore} from "@/store/user";
const ss = inject('socket')
const activities = ref([])
const content = ref('')
//使用用户信息存储
const userStore = useUserStore();
const username = userStore.getUserInfo()?.username;
const onMessage = (msg: any)=> {
  console.log(msg)
  if (msg.app === 'chat' && msg.operation === 'sendMsg'){
    activities.value.push({
      from: msg.authToken,
      content: msg.data,
      timestamp: msg.timestamp
    })
  }
}
ss.on('message', onMessage)
const send = ()=>{
  const msg = reactive<SocketData>(chat_send);
  msg.authToken = username
  msg.data.msg = content
//  msg.data.userIds = ["wxd"];
  ss.send(msg)
  const msgContent = content.value
  activities.value.push({
    from: username,
    content: msgContent,
    timestamp: getFullTime()
  })
  content.value = ''
}
onMounted(() => {

});
</script>

<template>
  <div>
    <div class="block">
      <div class="chat-container">
        <div class="chat-header">
          聊天窗口
        </div>
        <div ref="chatWindow" class="chat-window">
          <div v-for="(msg, index) in activities" :key="index" class="message">
            <div class="message-header">
              {{ msg.timestamp }} - {{ msg.from }}
            </div>
            <div :class="['message-content', msg.from === username ? 'self' : 'other']">
              {{ msg.content }}
            </div>
          </div>
        </div>
        <div class="input-area">
          <input v-model="content" @keyup.enter="send" placeholder="输入消息...">
          <button @click="send">发送</button>
        </div>
      </div>
    </div>
  </div>
</template>
<style scoped>
.chat-container {
  width: 80vw;
  /* 80% of viewport width */
  max-width: 800px;
  /* 最大宽度 */
  height: 95vh;
  /* 80% of viewport height */
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.chat-header {
  background-color: #4a4a4a;
  color: #fff;
  padding: 10px;
  /* 减少内边距 */
  text-align: center;
  font-size: 18px;
  line-height: 1;
  /* 确保一行显示 */
  height: 40px;
  /* 固定高度 */
}

.chat-window {
  flex: 1;
  padding: 15px;
  overflow-y: scroll;
  border-bottom: 1px solid #e0e0e0;
}

.message {
  margin-bottom: 10px;
  display: flex;
  flex-direction: column;
}

.message-header {
  font-size: 12px;
  color: #777;
  margin-bottom: 4px;
  margin-inline: auto;
}

.message-content {
  max-width: 50%;
  padding: 10px;
  border-radius: 15px;
  position: relative;
  word-wrap: break-word;
  white-space: pre-wrap;
  /* 允许换行 */
  line-height: 0.5;
  /* 调整行高 */
}

.message-content.self {
  background-color: #dcf8c6;
  align-self: flex-end;
}

.message-content.other {
  background-color: #f1f0f0;
  align-self: flex-start;
}

.message-content.self::before {
  content: '';
  position: absolute;
  right: -10px;
  top: 10px;
  border-width: 10px 0 10px 10px;
  border-style: solid;
  border-color: transparent transparent transparent #dcf8c6;
}

.message-content.other::before {
  content: '';
  position: absolute;
  left: -10px;
  top: 10px;
  border-width: 10px 10px 10px 0;
  border-style: solid;
  border-color: transparent #f1f0f0 transparent transparent;
}

.input-area {
  display: flex;
  padding: 15px;
}

.input-area input {
  flex: 1;
  padding: 10px;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  outline: none;
}

.input-area button {
  margin-left: 10px;
  padding: 10px 15px;
  background-color: #4a4a4a;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.input-area button:hover {
  background-color: #3a3a3a;
}
</style>